import api from '@/store/api';

const sortObjectByKeys = (obj: { [index: string]: any }) => {
  const sortedKeys = Object.keys(obj).sort();

  const sortedObj = {} as any;
  sortedKeys.forEach((key) => {
    sortedObj[key] = obj[key];
  });

  return sortedObj;
};

export const usePerformanceMonitorStore = defineStore(
  'performanceMonitor',
  () => {
    const hostStatus = ref({} as { [index: string]: any });
    const setHostStatus = (status: any) => {
      hostStatus.value = status;
    };

    const getHostStatus = async () => {
      const chassisUrl = await api
        .get('/redfish/v1/Chassis/')
        .then(({ data: { Members } }) => Members[0]['@odata.id'])
        .catch((err) => {
          throw err;
        });
      return await api
        .get(`${chassisUrl}/HostStatus/`)
        .then(async ({ data: { Members } }) => {
          const hardwaresInfo = {} as any;
          const promises1 = [] as any[];
          Members.forEach(
            (member: {
              '@odata.id': string;
              Members?: { '@odata.id': string }[];
            }) => {
              const hardwareName =
                member['@odata.id'].split('/')[
                  member['@odata.id'].split('/').length - 1
                ];
              const url = member['@odata.id'];
              const promise1 = api
                .get(url)
                .then(async ({ data }) => {
                  if (!Object.hasOwnProperty.call(data, 'Members')) {
                    const temp = {} as any;
                    temp[hardwareName] = data.Data;
                    hardwaresInfo[hardwareName] = temp;
                  } else {
                    const temp = {} as any;
                    const promises2 = [] as any[];
                    data.Members!.forEach((item: { '@odata.id': string }) => {
                      const compName =
                        item['@odata.id'].split('/')[
                          item['@odata.id'].split('/').length - 1
                        ];
                      const promise2 = api
                        .get(item['@odata.id'])
                        .then(({ data: { Data } }) => {
                          temp[compName] = Data;
                        });
                      promises2.push(promise2);
                    });
                    await Promise.all(promises2);
                    hardwaresInfo[hardwareName] = sortObjectByKeys(temp);
                  }
                })
                .catch((err) => {
                  throw err;
                });
              promises1.push(promise1);
            },
          );
          await api.all(promises1).catch((err) => {
            return err;
          });
          setHostStatus(sortObjectByKeys(hardwaresInfo));
        })
        .catch((error) => {
          throw error;
        });
    };
    return { hostStatus, setHostStatus, getHostStatus };
  },
);
